/*
 * @lc app=leetcode.cn id=24 lang=java
 *
 * [24] 两两交换链表中的节点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        if ( head == null){
            return head;
        }
        if ( head.next == null){
            return head;
        }
        int count = 0;
        ListNode beforeHead = new ListNode(0);
        beforeHead.next = head;
        ListNode beforeHeadDe = beforeHead;
        ListNode temp = head;
        while (beforeHeadDe.next != null ){
            ListNode temp1 = beforeHeadDe.next;
            if ( temp1.next == null){
                break;
            }
            ListNode temp2 = temp1.next;
            temp1.next = temp2.next;
            beforeHeadDe.next = temp2;
            temp2.next = temp1;
            beforeHeadDe  = beforeHeadDe.next.next;
        }
        return beforeHead.next;
    }
}
// @lc code=end

